package study.dq.unit.test.service;

import study.dq.unit.test.dao.IUserDao;
import study.dq.unit.test.model.User;
import study.dq.unit.test.model.UserException;

public class UserService implements IUserService {
    private IUserDao userDao;

    public UserService(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User load(String username) {
        return userDao.load(username);
    }

    @Override
    public User add(String username, String password, String nickname) {
        User user = userDao.load(username);
        if (user!=null)throw new UserException("用户已存在");
        return userDao.add(User.builder().username(username).password(password).nickname(nickname).build());
    }

    @Override
    public void delete(String username) {
        userDao.delete(username);
    }

    @Override
    public User update(String username, String password, String nickname) {
        return userDao.update(username,password,nickname);
    }

    @Override
    public boolean login(String username, String password) {
        User user =  userDao.load(username);
        if (user==null){
            return false;
        }else return password.equals(user.getPassword());
    }
}
